package com.eltsistemas.adapter;

import java.net.URL;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;

public class Relatorio {

	public JasperReport getReport(String caminho) throws Exception
	{
		URL url = this.getClass().getClassLoader().getResource(caminho);	
		JasperReport relatorioJasper = (JasperReport) JRLoader.loadObject(url);// O objeto do tipo JasperReport pega a url
				
		return relatorioJasper;
	}
	
	public boolean geraRelatorio(byte[] bytes, HttpServletResponse response) throws Exception
	{
		if (bytes != null && bytes.length > 1000)
		{ 

			response.setContentType("application/pdf");
			response.setContentLength(bytes.length);
			ServletOutputStream ouputStream = response.getOutputStream();
			ouputStream = response.getOutputStream();
			ouputStream.write(bytes, 0, bytes.length);
			ouputStream.flush();
			ouputStream.close();
			
			return true;
		}
		
		return false;
	}	

}
